package com.example.archermind.myapplication;

import android.androidcontrol.IAndroidControlService;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AndroidToLinuxActivity extends BaseActivity implements AdapterView.OnItemClickListener {


    @BindView(R.id.gv_androittolinux)
    GridView gvAndroidToLinux;
    @BindView(R.id.et_dialPhoneNumber)
    EditText etDialPhoneNumber;
    @BindView(R.id.btn_dialPhoneNumber)
    Button btnDialPhoneNumber;
    @BindView(R.id.tv_dialPhoneNumber)
    TextView tvDialPhoneNumber;
    @BindView(R.id.et_longitude)
    EditText etLongitude;
    @BindView(R.id.et_latitude)
    EditText etLatitude;
    @BindView(R.id.et_label)
    EditText etLabel;
    @BindView(R.id.btn_navigationToPOI)
    Button btnNavigationToPOI;
    private String[] names = new String[]{
            "NAVIGATION_MAIN                         ",
            "GENERIC_BACK                            ",
            "HOME_SCREEN                             ",
            "GRID_MENU                               ",
            "PHONE_MAIN                              ",
            "PHONE_SETUP                             ",
            "PHONE_SETUP_SELECTION_VIEW              ",
            "TUNER_ENTRY                             ",
            "TUNER_FM_MAIN                           ",
            "TUNER_AM_MAIN                           ",
            "TUNER_FM_SETUP                          ",
            "TUNER_AM_SETUP                          ",
            "MEDIA_ENTRY                             ",
            "MEDIA_BT_MAIN                           ",
            "MEDIA_JUKEBOX_MAIN                      ",
            "MEDIA_SETUP                             ",
            "PICTURE_VIEWER_MAIN                     ",
            "CAR_MAIN                                ",
            "CAR_SETUP_MAIN                          ",
            "FAS_HOME                                ",
            "CLIMATE_DESKTOP                         ",
            "SOUND_EQ                                ",
            "APP_CONNECT_DESKTOP                     ",
            "USER_MANAGEMENT_MAIN                    ",
            "SYSTEM_SETUP_MAIN                       ",
            "SYSTEM_SETUP_SDS                        ",
            "SYSTEM_SETUP_GENERAL                    ",
            "SYSTEM_SETUP_EXTERNAL_APPS              ",
            "SYSTEM_SETUP_WLAN_CLIENT                ",
            "SYSTEM_SETUP_WLAN_DESKTOP_HOTSPOT_MAIN  ",
            "CONNECTION_MANAGER                      ",


    };

    private Handler mHanlder = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle bundle = msg.getData();
            int reslut = bundle.getInt("reslut");
            switch (msg.what) {
                case DIALPHONENUMBERBARCALLBACK + 0:
                    tvDialPhoneNumber.setText("result:" + reslut);
                    break;

            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_to_linux);
        unbinder = ButterKnife.bind(this);
        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, names);
        gvAndroidToLinux.setAdapter(arrayAdapter);
        gvAndroidToLinux.setOnItemClickListener(this);
        setHandler(mHanlder);
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        IAndroidControlService androidControlService = getAndroidControlService();
        try {
            androidControlService.switchToLinux(i + 4);//从下标4开始计算
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.btn_dialPhoneNumber,R.id.btn_navigationToPOI})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_dialPhoneNumber:
                String number = etDialPhoneNumber.getText().toString();
                if (!TextUtils.isEmpty(number)) {
                    try {
                        getAndroidControlService().dialPhoneNumber(number, new DialPhoneNumberBarCallback(this));
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.btn_navigationToPOI:
                double latitude = Double.parseDouble(etLatitude.getText().toString());
                double longutude = Double.parseDouble(etLongitude.getText().toString());
                String label = etLabel.getText().toString();
                try {
                    getPoiService().navigationToPOI(longutude,latitude,label);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
